package itheima.edu.info.manager.service;

import itheima.edu.info.manager.dao.StudentDao;
import itheima.edu.info.manager.domain.Student;

public class StudentService{
    //业务员
    //1.创建一个StudentDao库管对象
    private StudentDao studentDao = new StudentDao();
    public boolean addStudent(Student stu) {

        //2.将获取到的Student对象传入到StudentDao中
        //3.将StudentDao中返回的结果返回给方法调用者
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
        StudentDao studentDao = new StudentDao();
        Student[] stus = studentDao.findAllStudent();
        //假设id在数组中不存在
        boolean exists = false;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Student[] findAllStudent() {
        //1.调用库管对象的findAllStudent获取学生对象数组
        Student[] allStudent=studentDao.findAllStudent();
        //2.判断数组中是否有学生信息（有：返回地址， 没有返回null）
        //数组中只要存在一个不是null的元素 ，那就代表有学生信息
        boolean flag=false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu=allStudent[i];
            if (stu!=null){
                flag=true;
                break;
            }
        }
        if (flag){
            return allStudent;
        }else{
            return null;
        }
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
